package community.hospital.service.domain.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import community.hospital.common.CustomerException;
import community.hospital.convert.PageConvert;
import community.hospital.convert.UserConvert;
import community.hospital.entity.User;
import community.hospital.entity.request.ListUserRequest;
import community.hospital.entity.response.UserResponse;
import community.hospital.enums.BusinessEnum;
import community.hospital.service.domain.UserService;
import community.hospital.mapper.UserMapper;
import community.hospital.util.MD5Util;
import community.hospital.util.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;

import static community.hospital.enums.BusinessEnum.USER_NOT_FOUND;

/**
* @author ydj
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-01-16 16:35:35
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public Page<UserResponse> listUser(ListUserRequest request) {
        Page<User> page = new Page<>(request.getPageNumber(),request.getPageSize());
        lambdaQuery()
                .eq(StringUtils.isNotBlank(request.getAccount()), User::getAccount, request.getAccount())
                .eq(StringUtils.isNotBlank(request.getUsername()), User::getUsername, request.getUsername())
                .eq(StringUtils.isNotBlank(request.getPhone()), User::getPhone, request.getPhone())
                .page(page);

        List<UserResponse> results = page.getRecords().stream().map(UserConvert.INSTANCE::convertToResp).collect(Collectors.toList());
        return PageConvert.convert(page,results);
    }

    @Override
    public int countByIdCard(String idCard) {
        Long count = lambdaQuery()
                .eq(User::getIdCard, idCard)
                .count();
        return count.intValue();
    }

    @Override
    public int countByPhone(String phone) {
        Long count = lambdaQuery()
                .eq(User::getPhone, phone)
                .count();
        return count.intValue();
    }

    @Override
    public int countByAccount(String account) {
        Long count = lambdaQuery()
                .eq(User::getAccount, account)
                .count();
        return count.intValue();
    }

    @Override
    public User findUserByAccount(String account) {
        return lambdaQuery().eq(User::getAccount, account).one();
    }


}




